module Explode.GameoverState;
private import Explode.Stage;
private import Explode.Player;
private import Explode.GameConfig;
private import Explode.GameState;
private import Form.MenuItem;
private import State.IState;
private import Input.KeyCode;
private import Input.GamePad;
private import Graphics.IGraphics;
private import System.Console;
private import Sound.Manager;
private import std.file;


/**
*	ゲームオーバーメニュー
*/
private class GameoverState : BasicState {
	private GameStage _stage;
	private GameParam _param;
	private IState _caller;
	private Player _player;
	private int _frameCnt;
	protected IGamePad _gamepad;
	protected IMenuItem[] _items; 
	protected MenuItem _continueMenu , _titleMenu;
	
	protected GameState _gameState;
	protected IMenuItem _activeItem ;
	protected IState _nextState ;
	
	protected int _nextFlag = 0;
	public int nextFlag(){ return _nextFlag;}
	public const int GAME_CONTINUE = 0;
	public const int GAME_QUIT     = 1;
	protected const int WIDTH = 240 , HEIGHT = 480;

	public this( GameState state , GameParam param , IGamePad pad){
		_gameState = state;
		_param     = param;
		_gamepad   = pad;
	} //

	public IState call( IState caller ) {
		_caller = caller;
		_nextState = this;
		_gamepad.reset();
		initMenu();
		SoundManager.background.stop();
		_frameCnt = 0;
		return this;
	} //
	
	private void initMenu(){
		if ( _continueMenu is null ) {
			Console.write("Create Gameover Menu...");
			_continueMenu  = new MenuItem("CONTINUE");
			_titleMenu     = new MenuItem("TITLE");
			
			_items = [ _continueMenu ,_titleMenu ];
			MenuItemConnector.connectVertical( _items );
			_activeItem = _continueMenu.activate();
			
			int xx = WIDTH / 2 , yy = HEIGHT /2;
			_continueMenu.moveTo(  xx , yy - 25 );
			_titleMenu.moveTo(     xx , yy + 25 );

			_continueMenu.setSelectHandler( &onContinueMenuSelect );
			_titleMenu.setSelectHandler(    &onTitleMenuSelect );

			Console.writeln("OK");
		}
	} //

	int _cntY = 0, _cntX = 0;
	public IState action() {
		_frameCnt ++;
		foreach ( int i , IMenuItem item ; _items  ) {
			item.action();
		} //
		if ( _frameCnt > 30 ) {
			if ( _gamepad.y != 0 )  _cntY++; 
			else					_cntY = 0;
			if ( _gamepad.x != 0 )  _cntX++;
			else					_cntX = 0;
			if ( _cntY % 10 == 1 ) {
				_activeItem = ( _gamepad.y > 0 ) ? _activeItem.down  : _activeItem.up;
			}
			if ( _cntX % 10 == 1 ) {
				_activeItem = ( _gamepad.x > 0 ) ? _activeItem.right : _activeItem.left;
			}
		}
		if ( _gamepad.getButton( 0 ) != 0 || _gamepad.getButton( 1 ) != 0 || _gamepad.getButton( 9 ) != 0) {
			_activeItem.select();
		}
		return _nextState;
	} //

	public IState draw( IGraphics g ) {
		foreach ( int i , IMenuItem item ; _items  ) {
			item.drawingEntry( g );
		} //

		g.setViewport( 0 , 0, WIDTH , HEIGHT );
		Color cls = Color.create( 0x08000000u);
		g.drawFilledRect( 32 , 32 , WIDTH - 64 , HEIGHT - 64  , cls );
		g.drawText( "GAME  OVER" , ( WIDTH - 160 )/2 , 100 , Color.create( 0xffffff00u ) , 16 , 48 );
		g.draw();

		return this;
	} //

	public bool onKeyDown( int keyCode ){
		switch ( keyCode ) {
			case KeyCode.UP:
				_gamepad.y = -1;
				break;
			case KeyCode.DOWN:
				_gamepad.y =  1;
				break;
			case KeyCode.LEFT:
				_gamepad.x = -1;
				break;
			case KeyCode.RIGHT:
				_gamepad.x =  1;
				break;
			case KeyCode.Z:
				_gamepad.setButton( 0 , 1 );
				break;
			case KeyCode.X:
				_gamepad.setButton( 1 , 1 );
				break;
			default:
				break;
		}
		return true;
	} //
	public bool onKeyUp( int keyCode ){
		switch ( keyCode ) {
			case KeyCode.UP:
				_gamepad.y = 0;
				break;
			case KeyCode.DOWN:
				_gamepad.y = 0;
				break;
			case KeyCode.LEFT:
				_gamepad.x = 0;
				break;
			case KeyCode.RIGHT:
				_gamepad.x = 0;
				break;
			case KeyCode.Z:
				_gamepad.setButton( 0 , 0 );
				break;
			case KeyCode.X:
				_gamepad.setButton( 1 , 0 );
				break;
			default:
				break;
		}
		return true;
	} //
	public bool onJoyAxis( int deviceId , int axis , int delta ){
		if ( axis == JOYAXIS_X ) {
			if( delta != 0 ) _gamepad.x = ( delta > 0 ) ? 1 : -1;
			else			 _gamepad.x = 0;
		}
		if ( axis == JOYAXIS_Y ){
			if( delta != 0 ) _gamepad.y = ( delta > 0 ) ? 1 : -1;
			else			 _gamepad.y = 0;
		}
		return true;
	} //
	
	public bool onJoyButtonDown( int id , int button ) {
		_gamepad.setButton( GameConfig.getButtonMap( button ) , 1 );
		return true;
	} //

	public bool onJoyButtonUp( int id , int button ) {
		_gamepad.setButton( GameConfig.getButtonMap( button ) , 0 );
		return true;
	} //


	public void onContinueMenuSelect( IMenuItem im ) {
		_nextFlag  = GAME_CONTINUE;
		_nextState = _gameState.call( this );
		Console.writeln("CONTINUE GAME!");
	} //
	
	public void onTitleMenuSelect(IMenuItem im){
		_nextFlag  = GAME_QUIT;
		_nextState = _gameState.call( this );
		Console.writeln("TITLE!");
	}


} //



